package org.jtb.quickr;

import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import org.jdom.Element;

/**
 * Person response.
 *
 * @see org.jtb.quickr.PeopleService#getInfo(String)
 */
public class PersonResponse extends FlickrResponse {
    /**
     * Person photos response.
     */
    public static class Photos extends FlickrResponse {
        private long firstDate;
        private String firstDateTaken;
        private int count;
        
        public Photos(Element e) throws QuickrException {
            super(e);
        }
        
        protected void populate(Element e) {
            String firstDateString = getBodyString(e, "firstdate");
            if (firstDateString != null) {
                this.firstDate = Long.valueOf(firstDateString).longValue();
            }
            String firstDateTaken = getBodyString(e, "firstdatetaken");
            if (firstDateTaken != null) {
                this.firstDateTaken = firstDateTaken;
            }
            String countString = getBodyString(e, "count");
            if (countString != null) {
                this.count = Integer.valueOf(countString).intValue();
            }
        }
        
        public long getFirstDate() {
            return firstDate;
        }
        
        public String getFirstDateTaken() {
            return firstDateTaken;
        }
        
        public int getCount() {
            return count;
        }
    }
        
    private String nsId;
    private boolean admin;
    private boolean pro;
    private int iconServer;
    private String userName;
    private String realName;
    private String mboxSha1Sum;
    private String location;
    private URL photosUrl;
    private URL profileUrl;
    private Photos photos;
    private String staticUrl;
    
    PersonResponse(InputStream is, String staticUrl) throws QuickrException {
        super(is);
        this.staticUrl = staticUrl;
    }
    
    protected void populate(Element e) throws QuickrException {
        Element userElement = e.getChild("person");
        
        this.nsId = userElement.getAttributeValue("nsid");
        this.admin = getAttrBoolean(userElement, "isadmin");
        this.pro = getAttrBoolean(userElement, "ispro");
        this.iconServer = getAttrInt(userElement, "iconserver");
        
        String userName = getBodyString(userElement, "username");
        if (userName != null) {
            this.userName = userName;
        }
        String realName = getBodyString(userElement, "realname");
        if (realName != null) {
            this.realName = realName;
        }
        String mboxSha1Sum = getBodyString(userElement, "mbox_sha1sum");
        if (mboxSha1Sum != null) {
            this.mboxSha1Sum = mboxSha1Sum;
        }
        String location = getBodyString(userElement, "location");
        if (location != null) {
            this.location = location;
        }
        try {
            String photosUrlString = getBodyString(userElement, "photosurl");
            if (photosUrlString != null) {
                this.photosUrl = new URL(photosUrlString);
            }
            String profileUrlString = getBodyString(userElement, "profileurl");
            if (profileUrlString != null) {
                this.profileUrl = new URL(profileUrlString);
            }
        } catch (MalformedURLException mfue) {
            // nothing
        }
        Element photosElement = e.getChild("photos");
        if (photosElement != null) {
            this.photos = new Photos(photosElement);
        }
    }
    
    public String getNsId() {
        return nsId;
    }
    
    public boolean isAdmin() {
        return admin;
    }
    
    public boolean isPro() {
        return pro;
    }
    
    public int getIconServer() {
        return iconServer;
    }
    
    public String getUserName() {
        return userName;
    }
    
    public String getRealName() {
        return realName;
    }
    
    public String getMboxSha1Sum() {
        return mboxSha1Sum;
    }
    
    public String getLocation() {
        return location;
    }
    
    public URL getPhotosUrl() {
        return photosUrl;
    }
    
    public URL getProfileUrl() {
        return profileUrl;
    }
    
    public Photos getPhotos() {
        return photos;
    }
    
    public URL getBuddyIconUrl() throws QuickrException {
        return new BuddyIconUrl(staticUrl, iconServer, nsId).getUrl();
    }
}
